/// @brief BinaryIndexedArray
#include <algorithm>
#include <cctype>
#include <cstdio>
#define lowbit(x) (x & -x)

typedef long long ll;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

const int N = 3e5 + 5;
int n, l, r, val[N], h[N], p[N];
ll ans;

inline void modify(int x) {
  for (int i = x; i <= n; i += lowbit(i)) ++val[i];
}

inline int query(int x) {
  int res = 0;
  for (int i = x; i; i -= lowbit(i)) res += val[i];
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("swp.in", "r", stdin);
  freopen("swp.out", "w", stdout);
#endif
#endif

  read(n);
  for (int i = 1; i <= n; i++) read(h[i]), p[i] = i;
  std::sort(p + 1, p + n + 1,
            [](const int lhs, const int rhs) { return h[lhs] > h[rhs]; });
  for (int i = 1, j, k; i <= n;) {
    for (j = i; j <= n; ++j) {
      k = query(p[j]);
      ans += std::min(k, i - 1 - k);
      if (h[p[j + 1]] != h[p[j]]) break;
    }
    for (; i <= j; ++i) modify(p[i]);
  }
  printf("%lld\n", ans);
  return 0;
}